package com.ai.service.rocketMq;

import com.ai.common.constant.IndustryInfoConstant;
import com.ai.common.constant.enumP.OperateTypeEnum;
import com.ai.entity.Industry.IndustryMQEntity;
import com.ai.service.security.ISysUserService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 工业互联网，人员基础信息变更推送
 * @Author jiaok
 * @Date 2024-02-01 14:59
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "qbee-service-100eefd2cd504895a183as931d878468-1749331438301368320",selectorExpression = "*",
        consumeMode = ConsumeMode.ORDERLY,consumerGroup = "Industry_Info")
public class RocketMQIndustryInfoConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public void onMessage(MessageExt messageExt) {
        String tags = messageExt.getTags();

        String messageBody = new String(messageExt.getBody());
        JSONObject messageJson = JSONObject.parseObject(messageBody);

        String tag = (String) messageJson.get("tag");
        Integer operateType = (Integer) messageJson.get("operateType");

        if (tag.equals(IndustryInfoConstant.EMPLOYEE_TAG)){
            //获取mq中的消息体
            IndustryMQEntity industryMQEntity = JSONObject.parseObject((String) messageJson.get("body"), IndustryMQEntity.class);
            if (operateType.equals(OperateTypeEnum.ADD.getValue())){
                //用户新增操作
                log.info("===========================  监听到mq中新增用户消息  ===========================");
                Boolean aBoolean = iSysUserService.rocketMqUserAdd(industryMQEntity);
            }else if (operateType.equals(OperateTypeEnum.UPDATE.getValue())){
                //用户更新
                log.info("===========================  监听到mq中修改用户信息  ===========================");
                Boolean aBoolean = iSysUserService.rocketMqUserUpdate(industryMQEntity);
            } else if (operateType.equals(OperateTypeEnum.DELETE.getValue())) {
                //用户删除
                log.info("===========================  监听到mq中删除用户信息   ===========================");
                Boolean isDel =  iSysUserService.rocketMqUserDel(industryMQEntity);
            }
        }

    }
}
